#include <iostream>
#include <vector>
#include <string>
using namespace std;

// test1一些简单的vector类数组操作
void test1(){
    //创建一个vector数组
    vector<int> nums = {1,2,3,4,5,6};
    //vector数组有跟普通数组一样的遍历方式
    for(int i = 0; i < 6; i++){
        cout << nums[i] << endl;
    }
    //还有很多不同于普通数组的遍历方式
    //1.增强for循环遍历方式
    for(auto & ele : nums){
        cout << ele <<  " ";
    }
    cout << endl;
    cout << "=============" << endl;
    //2.迭代器遍历方式
    auto it = nums.begin();
    while(it != nums.end()){
        cout << *it << " ";
        it++;
    }
    cout << endl;
cout << "---------------" << endl;
}

// vector常用操作测试
void test2(){
    vector<int> number;
    cout << number.size() << endl;
    cout << number.capacity() << endl;
    // push_back()函数在vector末尾添加元素
    number.push_back(1);
    cout << number.size() << endl;
    cout << number.capacity() << endl;
    //初设无初始化长度，每次当vector内存容量不足时
    //vector会主动的“二倍扩增”容量
    number.push_back(2);
    cout << number.size() << endl;
    cout << number.capacity() << endl;
    //此时capacity的大小为2要是再次执行push操作，容量会扩增为4
    number.push_back(3);
    cout << number.size() << endl;
    cout << number.capacity() << endl;
    number.push_back(4);
    cout << number.size() << endl;
    cout << number.capacity() << endl;
    //在vector这个数组的末尾删除一个元素，没有参数
    number.pop_back();
    cout << number.size() << endl;
    cout << number.capacity() << endl;
    cout << "=======================" << endl;
    //对于vector的底层实现来说，其定义的内存空间一定在堆空间上
    //而且vector容器必然会出现三个指针（ _start  _finish  _end_of_storage）
    // .size() 的操作实际上输出的长度是 _finish - _start
    // 而对于 .capacity() 的操作的实际输出的内容是 _end_of_storage - start
    cout << "还有一些其他vector的函数" << endl;
    // reserve() 主动申请空间大小
    vector<int> number2;
    number2.reserve(10);
    cout << number2.size() << endl;
    cout << number2.capacity() << endl;
    // shrink_to_fit()函数 空间释放函数，释放多余未使用空间
    cout << "shrink_to_fit" << endl;
    number2.push_back(1);
    number2.push_back(2);
    number2.push_back(3);
    number2.shrink_to_fit();
    cout << number2.size() << endl;
    cout << number2.capacity() << endl;
    // clear()函数 清空所有元素，但不释放空间
    cout << "clear" << endl;
    number2.clear();
    cout << number2.size() << endl;
    cout << number2.capacity() << endl;

}

int main(int atgc, char * argv[])
{
    test1();
    test2();
    return 0;
}

